﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Server
{
    /// <summary>
    /// This class represents a single user in our application.
    /// </summary>
    public class User
    {
        /// <summary>
        /// The user id
        /// </summary>
        private string uid;
        public string Uid
        {
            get { return uid; }
            set { uid = value; }
        }

        /// <summary>
        /// The password
        /// </summary>
        private string password;
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        /// <summary>
        /// The connection for the user
        /// </summary>
        private ClientHandler connection;

        /// <summary>
        /// A flag true if this user is logged in correctly
        /// </summary>
        private bool isLoggedIn;

        /// <summary>
        /// A flag true if this user is online and connected
        /// </summary>
        private bool isConnected;

        /// <summary>
        /// Property true if this user connected
        /// </summary>
        [XmlIgnore]
        public bool IsConnected
        {
            get { return isConnected; }
            set { isConnected = value; }
        }

        /// <summary>
        /// Property true if this user logged in correctly
        /// </summary>
        [XmlIgnore]
        public bool IsLoggedIn
        {
            get { return isLoggedIn; }
            set { isLoggedIn = value; }
        }

        /// <summary>
        /// Property for the user name
        /// </summary>
        [XmlAttribute]
        public string Name
        {
            get { return uid; }
        }

        /// <summary>
        /// Creates a new instance of the User
        /// </summary>
        /// <param name="id">The user id</param>
        /// <param name="pd">The password</param>
        /// <param name="connect">The connection for this user</param>
        public User(string id, string pd, ClientHandler connect)
        {
            this.uid = id;
            this.password = pd;
            this.connection = connect;
            this.isLoggedIn = false;
            this.isConnected = true;
        }

        /// <summary>
        /// Parameterless constructor because XML is moody
        /// </summary>
        public User()
        {
            this.isLoggedIn = false;
            this.isConnected = false;
        }

        /// <summary>
        /// Check the password for the user
        /// </summary>
        /// <param name="pass">The password to use</param>
        /// <returns>True if actual password matches the submitted one, false otherwise</returns>
        public bool Authenticate(string pass)
        {
            return (this.password == pass);
        }

        public bool Authenticate(string pass, ClientHandler connect)
        {
            this.isConnected = true;
            this.connection = connect;

            return Authenticate(pass);
        }

        /// <summary>
        /// Send a message to this user
        /// </summary>
        /// <param name="message">the message to send</param>
        public void SendMessage(string message)
        {
            this.connection.SendResponse(message);
        }

    }
}
